package Game;

import javax.media.opengl.GL2;
import javax.media.opengl.glu.GLU;
import javax.media.opengl.glu.GLUquadric;

import Physics.CollisionBox;
import Physics.Matrix3;
import Physics.RigidBody;
import Physics.Vector3;

public class Box extends CollisionBox implements IGameObject 
{

	////////////////////////////
	// Member variables
	////////////////////////////
	ObjectType		m_objectType;
	//D3DXMATRIX	m_scale;
	int				m_health;
	ObjectMaterial	m_material;
	boolean			m_showHealth;
	boolean			m_hasHealth;
	Vector3			m_scale;
	
	////////////////////////////
	// Member functions
	////////////////////////////
	Box (float width, float height, float depth, ObjectMaterial material)
	{
		m_hasHealth = true;
		m_showHealth = false;
		m_body = new RigidBody();
		m_health = 100;
		m_material = material;
		
		super.m_halfSize = new Vector3(width, height, depth);
		
		float mass = width * height * depth * ObjectData.getDensity(material);
		m_body.setMass(mass);
		
		Matrix3 tensor = new Matrix3();
		tensor.setBlockInertiaTensor(super.m_halfSize,  mass);
		m_body.setInertiaTensor(tensor);
		
		m_body.setDamping(0.8f, 0.8f);
		m_body.setAwake(true);
		m_body.setMaterial(m_material);
		
		switch(m_material)
		{
		case STONE:
			m_objectType = ObjectType.OBJECT_STONEBOX;
			break;
			
		case WOOD:
			m_objectType = ObjectType.OBJECT_WOODBOX;
			break;
			
		case SAND:
			m_objectType = ObjectType.OBJECT_SANDBOX;
			break;
			
		case EXPLOSIVE:
			m_objectType = ObjectType.OBJECT_EXPLOSIVEBOX;
			break;
			
		default:
			m_objectType = ObjectType.OBJECT_DEFAULTBOX;
			break;		
		}
		
		// set the scaling matrix to the same as width, height, depth
		//D3DXMatrixScaling(&mScale, width, height, depth);
		m_scale = new Vector3(width, height, depth);
	}
	
	@Override
	public void render(GL2 gl, GLU glu) 
	{
		gl.glPushMatrix();
			gl.glLoadMatrixf(m_body.getGLMatrix(), 0);
			//gl.glTranslatef(m_body.getPosition().x, m_body.getPosition().y, 0);
			gl.glScalef(m_scale.x,  m_scale.y,  m_scale.z);
		 
			gl.glBegin(GL2.GL_QUADS);
			  //Quad 1
			    gl.glVertex3f( 1.0f, 1.0f, 1.0f);   //V2
			    gl.glVertex3f( 1.0f,-1.0f, 1.0f);   //V1
			    gl.glVertex3f( 1.0f,-1.0f,-1.0f);   //V3
			    gl.glVertex3f( 1.0f, 1.0f,-1.0f);   //V4
			  //Quad 2
			    gl.glVertex3f( 1.0f, 1.0f,-1.0f);   //V4
			    gl.glVertex3f( 1.0f,-1.0f,-1.0f);   //V3
			    gl.glVertex3f(-1.0f,-1.0f,-1.0f);   //V5
			    gl.glVertex3f(-1.0f, 1.0f,-1.0f);   //V6
			  //Quad 3
			    gl.glVertex3f(-1.0f, 1.0f,-1.0f);   //V6
			    gl.glVertex3f(-1.0f,-1.0f,-1.0f);   //V5
			    gl.glVertex3f(-1.0f,-1.0f, 1.0f);   //V7
			    gl.glVertex3f(-1.0f, 1.0f, 1.0f);   //V8
			  //Quad 4
			    gl.glVertex3f(-1.0f, 1.0f, 1.0f);   //V8
			    gl.glVertex3f(-1.0f,-1.0f, 1.0f);   //V7
			    gl.glVertex3f( 1.0f,-1.0f, 1.0f);   //V1
			    gl.glVertex3f( 1.0f, 1.0f, 1.0f);   //V2
			  //Quad 5
			    gl.glVertex3f(-1.0f, 1.0f,-1.0f);   //V6
			    gl.glVertex3f(-1.0f, 1.0f, 1.0f);   //V8
			    gl.glVertex3f( 1.0f, 1.0f, 1.0f);   //V2
			    gl.glVertex3f( 1.0f, 1.0f,-1.0f);   //V4
			  //Quad 6
			    gl.glVertex3f(-1.0f,-1.0f, 1.0f);   //V7
			    gl.glVertex3f(-1.0f,-1.0f,-1.0f);   //V5
			    gl.glVertex3f( 1.0f,-1.0f,-1.0f);   //V3
			    gl.glVertex3f( 1.0f,-1.0f, 1.0f);   //V1
			gl.glEnd();
		gl.glPopMatrix();
		
		/*gl.glPushMatrix();
			gl.glTranslatef(m_body.getPosition().x, m_body.getPosition().y, 0);
			
			 
			// Prepare light parameters.
	        float SHINE_ALL_DIRECTIONS = 1;
	        float[] lightPos = {-30, 0, 0, SHINE_ALL_DIRECTIONS};
	        float[] lightColorAmbient = {0.2f, 0.2f, 0.2f, 1f};
	        float[] lightColorSpecular = {0.8f, 0.8f, 0.8f, 1f};
	
	        // Set light parameters.
	        gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_POSITION, lightPos, 0);
	        gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_AMBIENT, lightColorAmbient, 0);
	        gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_SPECULAR, lightColorSpecular, 0);
	
	        // Enable lighting in GL.
	        gl.glEnable(GL2.GL_LIGHT1);
	        gl.glEnable(GL2.GL_LIGHTING);
	
	        // Set material properties.
	        float[] rgba = {1f, 1f, 1f};
	        gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_AMBIENT, rgba, 0);
	        gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_SPECULAR, rgba, 0);
	        gl.glMaterialf(GL2.GL_FRONT, GL2.GL_SHININESS, 0.5f);
	        
	        
	        // draw the sphere
	        //gl.glColor3f(0.3f, 0.5f, 1f);
	        GLUquadric earth = glu.gluNewQuadric();
	        glu.gluQuadricTexture(earth, true);
	        glu.gluQuadricDrawStyle(earth, GLU.GLU_FILL);
	        glu.gluQuadricNormals(earth, GLU.GLU_FLAT);
	        glu.gluQuadricOrientation(earth, GLU.GLU_OUTSIDE);
	        final float radius = 5.0f;
	        final int slices = 16;
	        final int stacks = 16;
	        glu.gluSphere(earth, radius, slices, stacks);
	        glu.gluDeleteQuadric(earth);
	    gl.glPopMatrix();*/
	}
	
	/////////////////////////////////
	// IGameObject implementation
	/////////////////////////////////
	@Override
	public RigidBody getBody() {
		return m_body;
	}

	@Override
	public ObjectMaterial getMaterial() {
		return m_material;
	}

	@Override
	public void specialAbility(FuriousTourist game) {
		// boxes don't have special abilities
	}

	@Override
	public void specialAbility2() {
		// boxes don't have special abilities
	}

	@Override
	public int getType() {
		return 0;
	}

	@Override
	public int getHP() {
		return m_health;
	}

	@Override
	public void setHP(int hp) {
		m_health = hp;
	}

	@Override
	public boolean hasHealth() {
		return m_hasHealth;
	}

	@Override
	public void toggleShowHealth() {
		if (hasHealth())
		{
			m_showHealth = !m_showHealth;
		}
	}

}
